{extend name="public:base" /}

{block name="css"}
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css"/>
<link rel="stylesheet" type="text/css" href="__ADMIN__/css/style.css">{/block}

{block name="content"}

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox ">
                <div class="ibox-content">
                    <form method="post">
                        <div class="box-item">
                            <div class="form-group row layui-form">
                                <label class="col-sm-1 col-form-label">所属分类</label>
                                <div class="col-sm-2">
                                    <select name="one_type_id" lay-filter="one_type_id">
                                    </select>
                                </div>
                                <div class="col-sm-2">
                                    <select name="two_type_id" lay-filter="two_type_id">
                                    </select>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>

                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">标题</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" name="title" placeholder="请输入标题" value="{$info.title}">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>

                        <div class="box-item">
                            <div class="form-group row">
                                <label class="col-sm-1 col-form-label">描述</label>
                                <div class="col-sm-4">
                                    <textarea class="form-control" name="desc" rows="8">{$info.desc}</textarea>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>

                        <input type="hidden" name="id" value="{$info['id']}">

                        <div class="form-group row">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button type="button" class="btn btn-w-m btn-default back-page">返回</button>
                                <button type="button" class="btn btn-w-m btn-primary ajax-post">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

{/block}

{block name='js'}
<script src="__STATIC__/layui/layui.js"></script>
<script>
    var type_list = `{$type_list}` ? {$type_list} : [];

    var one_type_id = `{$info['one_type_id']}` || 0;
    var two_type_id = `{$info['two_type_id']}` || 0;

    $(function () {
        layui.use(['form', 'layer'], function () {
            var form = layui.form;

            renderSelect(one_type_id, two_type_id);
            form.render();
        })
    })

    layui.use(['form', 'layer'], function () {
        var form = layui.form;

        form.on('select(one_type_id)', function (data) {
            var one_type_id = data.value;
            var index = data.elem.selectedIndex;

            var two_type_id = type_list[index].son_list[0].id;

            renderSelect(one_type_id, two_type_id);
            form.render();
        })
    })

    function renderSelect(selected_one_type_id, selected_two_type_id = 0) {
        var one_type_select = '';
        if (type_list.length > 0) {
            var son_list = [];
            type_list.forEach(val => {
                var one_selected = ''
                if (selected_one_type_id > 0) {
                    if (val.id == selected_one_type_id) {
                        one_selected = 'selected';
                        son_list = val.son_list;
                    }
                } else {
                    son_list = type_list[0].son_list;
                }

                one_type_select += `<option value="${val.id}" ${one_selected}>${val.name}</option>`
                var two_type_select = '';
                if (son_list && son_list.length > 0) {
                    son_list.forEach(v => {
                        var two_selected = selected_two_type_id == v.id ? 'selected' : '';
                        two_type_select += `<option value="${v.id}" ${two_selected}>${v.name}</option>`;
                    })
                }

                $('select[name="two_type_id"]').html(two_type_select);
            })
        }

        $('select[name="one_type_id"]').html(one_type_select);
    }

</script>{/block}
